package com.haowan.accessibilityservice;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initRecyclerView();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        gotoSettings();
    }

    public void onClick(View view) {
        Button btn = (Button) view;
        if (btn.getText().toString().equals("start")) {
            btn.setText("stop");
            startService(new Intent(this, GlobalActionBarService.class));
        } else {
            btn.setText("start");
            stopService(new Intent(this, GlobalActionBarService.class));
        }
    }

    public boolean checkAccessibilityEnabled(Context context) {
        int i = 0;
        boolean enable = false;
        try {
            i = Settings.Secure.getInt(context.getContentResolver(), "accessibility_enabled");
        } catch (Settings.SettingNotFoundException e) {
            Log.i(TAG, e.getMessage());
        }
        if (i == 1) {
            String string = Settings.Secure.getString(context.getContentResolver(), "enabled_accessibility_services");
            if (!TextUtils.isEmpty(string)) {
                String[] split = string.split(":");
                if (split.length > 0) {
                    for (String str : split) {
                        Log.d(TAG, str);
                        if (str.startsWith(context.getPackageName()) || str.equalsIgnoreCase(context.getPackageName() + "/" + GlobalActionBarService.class.getName())) {
                            enable = true;
                        }
                    }
                }
            }
        }
        return enable;
    }
    private void gotoSettings() {
        if (!checkAccessibilityEnabled(this)) {
            startActivity(new Intent("android.settings.ACCESSIBILITY_SETTINGS"));
        }
    }

    protected boolean canDrawOverlays() {
        boolean canDrawOverlays = true;
        if (Build.VERSION.SDK_INT >= 23) {
            canDrawOverlays = Settings.canDrawOverlays(this);
        }
        return canDrawOverlays;
    }


    public void gotoOverlaysSettings() {
        if (canDrawOverlays()) {
            return;
        }
        try {
            startActivityForResult(new Intent("android.settings.action.MANAGE_OVERLAY_PERMISSION", Uri.parse("package:" + getPackageName())), 1234);
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), "Request Permisssion Error", 0).show();
            e.printStackTrace();
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (requestCode == 1234 && canDrawOverlays()) {
            Log.i(TAG, "onActivityResult granted");
        }
    }

    public void onOneClick(View view) {
        Button button = (Button) view;
        Log.d(TAG, "onOneClick -> " + button.getText());
        switch (view.getId()) {
            case R.id.btn11:
                callPhone2("10086");
                break;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d(TAG, "onTouchEvent -> " + event.getX() + "," + event.getY());
        return super.onTouchEvent(event);
    }

    private List<ItemEntity> mList = new ArrayList<>();
    private void initRecyclerView(){
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        /*StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);*/
        recyclerView.setLayoutManager(layoutManager);
        MyAdapter adapter = new MyAdapter(mList);
        recyclerView.setAdapter(adapter);
    }

    private void initData() {
        for (int i = 0; i < 2; i++) {
            ItemEntity image0 = new ItemEntity("image0", R.mipmap.ic_launcher);
            mList.add(image0);
            ItemEntity image01 = new ItemEntity("image01", R.mipmap.ic_launcher);
            mList.add(image01);
            ItemEntity image02 = new ItemEntity("image02", R.mipmap.ic_launcher);
            mList.add(image02);
            ItemEntity image03 = new ItemEntity("image03", R.mipmap.ic_launcher);
            mList.add(image03);
            ItemEntity image04 = new ItemEntity("image04", R.mipmap.ic_launcher);
            mList.add(image04);
            ItemEntity image05 = new ItemEntity("image05", R.mipmap.ic_launcher);
            mList.add(image05);
            ItemEntity image06 = new ItemEntity("image06", R.mipmap.ic_launcher);
            mList.add(image06);
            ItemEntity image07 = new ItemEntity("image07", R.mipmap.ic_launcher);
            mList.add(image07);
            ItemEntity image08 = new ItemEntity("image08", R.mipmap.ic_launcher);
            mList.add(image08);
            ItemEntity image09 = new ItemEntity("image09", R.mipmap.ic_launcher);
            mList.add(image09);
            ItemEntity image10 = new ItemEntity("image10", R.mipmap.ic_launcher);
            mList.add(image10);
            ItemEntity image11 = new ItemEntity("image11", R.mipmap.ic_launcher);
            mList.add(image11);
            ItemEntity image12 = new ItemEntity("image12", R.mipmap.ic_launcher);
            mList.add(image12);
            ItemEntity image13 = new ItemEntity("image13", R.mipmap.ic_launcher);
            mList.add(image13);
        }
    }

    /**
     * 拨打电话（直接拨打电话）
     * @param phoneNum 电话号码
     */
    public void callPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }

    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    public void callPhone2(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }
}